package cs.rsa.ts14.juliet;

import java.text.DecimalFormat;

public class StandardCategoryConstructor implements CategorySyntaxConstructor {

	private static final String HEADER_STRING = "-- Time spent on classes and categories --";
	
	private static final DecimalFormat DF = new DecimalFormat("#.#");
	
	@Override
	public String buildHeaderLine() {
		StringBuilder sb = new StringBuilder();
		sb.append(HEADER_STRING).append(getLineBreak());
		return sb.toString();
	}

	@Override
	public String buildClassLine(String name, double hours, double percentage) {
		StringBuilder sb = new StringBuilder();
		sb.append(name).append(getClassLineTabString(name)).append(hours).append(" (").append(DF.format(percentage)).append("%)").append(getLineBreak());
		return sb.toString();
	}

	
	/**
	 * Evaluates how many tabs it is required to get the proper format for class line
	 * @param name The class name
	 * @return The tab string
	 */
	private String getClassLineTabString(String name) {
		if (name.length() < 8) {
			return "\t\t\t\t";
		}
		return "\t\t\t";
	}
	
	@Override
	public String buildCategoryLine(String name, double hours) {
		StringBuilder sb = new StringBuilder();
		sb.append("    ").append(name).append(getCategoryLineTabString(name)).append(":").append("\t").append(hours).append(getLineBreak());
		return sb.toString();
	}
	
	/**
	 * Evaluates how many tabs it is required to get the proper format for category line
	 * @param name The category name
	 * @return The tab string
	 */
	private String getCategoryLineTabString(String name) {
		if(name.length() < 4) {
			return "\t\t";
		}
		return "\t";
	}

	@Override
	public String buildTotalLine(double hourOne, double hourTwo) {
		StringBuilder sb = new StringBuilder();
		sb.append("Total:\t\t\t\t").append(hourOne).append(" (").append(hourTwo).append(")").append(getLineBreak());
		return sb.toString();
	}

	@Override
	public String buildEndLine() {
		StringBuilder sb = new StringBuilder();
		sb.append("\t\t\t\t").append("===============");
		return sb.toString();
	}

	@Override
	public String getLineBreak() {
		return "\n";
	}

}
